# -*- coding: utf8 -*-

from book.models import *
from google.appengine.ext import webapp
from book import utils
import logging

class UserPage(webapp.RequestHandler):
    def get(self):
        u"""用户列表"""
        
        users = User.all()        
        context = {
           'users' : users,
           'current_module' : 'user'
        }
        u"""察看并设置当天的收钱人"""
        todayChairMans = TodayChairMan.gql('WHERE date = :1', datetime.date.today()).fetch(1)
        if todayChairMans:
            for user in users:     
                try:           
                    if todayChairMans[0].user:
                        if user.key() == todayChairMans[0].user.key():
                            logging.getLogger().debug(u'=========find one=====');
                            context["chairmanKey"] = user.key()
                            break
                except:
                    pass        
        if len(self.request.path) > 5:            
            utils.render_to_response(self.response, "userlist.html", context)
        else:    
            utils.render_to_response(self.response, "user.html", context)
        
    def delete(self, key):
        u"""删除用户"""
        
        logging.getLogger().debug(u'删除用户： %s' % key);
        user = User.get(db.Key(key))
        if user:            
            orders = Order.gql('WHERE user = :1', user).fetch(1000)
            for order in orders:
                order.delete()
            
            u"""如果用户是当天的收钱人，将数据删除"""
            chairmans = TodayChairMan.gql('WHERE user = :1', user).fetch(1000)
            if chairmans:
                for chairman in chairmans:
                    chairman.delete()
            user.delete()        
            utils.render_to_success(self.response, u'删除用户成功')
        else:    
            logging.getLogger().debug(u'fail delete user')
            utils.render_to_error(self.response,'user_e003', u'删除用户失败')     
            
    def post(self):
        u"""设置收钱者 """
        
        user_key = self.request.get('user_key')
        logging.getLogger().debug(u'设置收钱者： %s' % user_key);
        user = User.get(db.Key(user_key))
        if user:
        	selfChairMan = TodayChairMan.gql('WHERE user = :1 and date = :2', user ,datetime.date.today()).fetch(1)
        	if selfChairMan:
        		u"""不想收钱"""  
        		selfChairMan[0].delete()
        		utils.render_to_success(self.response, u'你可以不用收钱了')
        	else:
        		u"""如果有人已经是当天的收钱人，则先将此人删除，再设置新的收钱人"""  
        		todayChairMans = TodayChairMan.gql('WHERE date = :1', datetime.date.today()).fetch(1)
        		if todayChairMans:
        			todayChairMans[0].delete()
        			tcm = TodayChairMan(user = user, isCharge = True, date = datetime.date.today())
        			tcm.put()
        			utils.render_to_success(self.response, u'设置收钱者成功')
        		else:
        			tcm = TodayChairMan(user = user, isCharge = True, date = datetime.date.today())
        			tcm.put()
        			utils.render_to_success(self.response, u'设置收钱者成功')
        else:    
            logging.getLogger().debug(u'fail delete user')
            utils.render_to_error(self.response,'user_e004', u'设置收钱者失败')
